import { computed, unref } from '@vue/runtime-core';
import moment from 'moment';
import { useStore } from 'vuex';
import { appStore } from '../store/modules/app';
import { PompeiLang, PompeiType } from '../types';
import { getDefaultTitle, getPompeiLang } from './usePrayers';

enum PompeiDays {
  Days = 27,
  everyDays = 9,
}

export interface StartDate {
  startDiffDay?: number;
  isPetition: boolean;
  whats?: number;
  nine?: number;
  startTitle: string;
  subTitle?: string;
  pompei: string;
}

export const getStartDate = computed(() => {
  return appStore.getStartDate;
});

// const store = useStore();
// const { startDate } = store.getters;

// const useStart = computed(() => {
//   return startDate;
// });

export function useDefaultDate(pompeiType: PompeiType = 'petition'): StartDate {
  const pompeiLang = getPompeiLang();
  const defaultTitle = getDefaultTitle().replace('type', pompeiLang[pompeiType]);

  appStore.commitStartDate({
    isPetition: pompeiType === 'petition',
    startTitle: defaultTitle,
    pompei: `pompei.${pompeiType}`,
  });

  return unref(getStartDate) as StartDate;
}

export function useStartDate(start_date: string): StartDate {
  let startDiffDay = moment().diff(moment(start_date), 'days') + 1;
  const isPetition: boolean = startDiffDay <= PompeiDays.Days;
  startDiffDay = !isPetition ? startDiffDay - 27 : startDiffDay;
  const whats: number = Math.floor(startDiffDay / PompeiDays.everyDays) + 1;
  const nine: number = startDiffDay % PompeiDays.everyDays;
  const startTitle = getStartTitle(isPetition, whats, nine);
  return {
    startDiffDay,
    isPetition,
    whats,
    nine,
    ...startTitle,
  };
}

function getStartTitle(
  isPetition: boolean,
  whats: number,
  nine: number
): Pick<StartDate, 'subTitle' | 'startTitle' | 'pompei'> {
  const subTitle = `${isPetition ? '求恩' : '谢恩'}第${whats}敬礼第${nine}天`;
  const startTitle = `庞贝圣母玫瑰经【${isPetition ? '求恩' : '谢恩'}】九日敬礼`;
  const pompei = `pompei.${isPetition ? 'petition' : 'grace'}`;
  return { startTitle, subTitle, pompei };
}
